package com.alix.paymoney.base;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;


import com.alix.paymoney.R;
import com.alix.paymoney.utils.AppManager;
import com.alix.paymoney.utils.SnackbarUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by 吴世芊 on 2016/10/26.
 * Update by  on
 * 杭州无尽信息有限公司
 * 邮箱：apple9302@126.com
 * 本类功能：
 */

public abstract class BaseActivity extends AppCompatActivity {

    private Toast toast;
    private Unbinder bind;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


//        StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.black_top));

        int layoutId = getLayoutId();

        setContentView(layoutId);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.colorPrimary));
            getWindow().setNavigationBarColor(ContextCompat.getColor(this, R.color.colorPrimary));
        }
        bind = ButterKnife.bind(this);
        /*getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);*/

        AppManager.getInstance().addActivity(this);
        afterCreate(savedInstanceState);
    }


    public Handler handler = new Handler();

    protected abstract int getLayoutId();

    protected boolean isScrollToBottom(RecyclerView recyclerView) {
        if (recyclerView == null) return false;
        if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange())
            return true;
        return false;
    }

    public void setRefreshFinish(SwipeRefreshLayout refreshLayout) {
        if (refreshLayout != null) {
            refreshLayout.setRefreshing(false);
        }
    }


    public void launcher(Class cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }

    public void launcherForResult(Class cls, int requstCode) {
        Intent intent = new Intent(this, cls);
        startActivityForResult(intent, requstCode);
    }

    public void launcher(Class cls, boolean showAnimation) {
        Intent intent = new Intent(this, cls);
        if (Build.VERSION.SDK_INT > 20) {
            if (showAnimation) {
                startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
            } else {
                startActivity(intent);
            }
        } else {
            startActivity(intent);
        }


    }

    public void launcher(Class cls, int code) {
        Intent intent = new Intent(this, cls);
        startActivityForResult(intent, code);
    }

    protected abstract void afterCreate(@Nullable Bundle savedInstanceState);

    //方法一：显示字符
    public void showToast(String msg) {

        toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public void showSnackBar(View view, String msg) {
        SnackbarUtils.showSnackBar(view, msg);
    }

    public void showOrangeSnackBar(View view, String msg) {
        SnackbarUtils.showSnackBar(view, msg, ContextCompat.getColor(this, R.color.colorPrimary));
    }

    public void showSnackBar(View view, String msg, int backgroundColor) {
        SnackbarUtils.showSnackBar(view, msg, backgroundColor);
    }

    public void showBottomSnackBar(View view, String msg) {
        SnackbarUtils.showBottomSnackBar(view, msg);
    }

    public void addStack(Activity activity) {
        AppManager.getInstance().addActivity(activity);
    }

    public void removeStack(Activity activity) {
        AppManager.getInstance().finishActivity(activity);
    }


    //方法二：显示value String里面的ResId字符串资源
    public void showToast(int ResId) {
        toast = Toast.makeText(this, ResId, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }
}
